var vm = new Vue({
    el:'#shop_detail',
    data(){
      return {
        host,
        username:'',
        sku_list:'',
        sku_id:'',
        sku_count: 1,
        cart_total_count: 0,
      }
    },
    mounted(){
        let user = localStorage.username;    
        
        this.username = user;  
          console.log(this.$route);
          this.get_sku_id();
          // this.username = user;
          this.get_shop_detail();
  
  
    },
    methods:{
       get_shop_detail:function(){
        // this.id = this.$route.query.sku_id;
            var url = this.host + '/show_detail/' + this.sku_id + '/'
            axios.get(url, {
                  responseType: 'json',
              })
                .then(response => {
                      this.sku_list = response.data;
                  
                  })
                  .catch(error => {
                      console.log(error.response);
                  })
         },
         // 从路径中提取sku_id  single-product.html?sku_id=1
          get_sku_id: function(){
            // console.log("document.location.pathname="+document.location.toString())
              var re = /\/Product_Detailed.html\?sku_id=(\d)$/;
              this.sku_id = document.location.toString().match(re)[1];
          },



          on_minus: function(){
            if (this.sku_count > 1) {
                this.sku_count--;
            }
          },
          on_addition: function(){
              if (this.sku_count < 20) {
                  this.sku_count++;
              }
          },

           add_cart: function(){
             let token =localStorage.token;
            var url = this.host + '/carts/'
            axios.post(url, 
                {
                    sku_id: parseInt(this.sku_id),
                    count: this.sku_count
                },
                {
                  headers: {
                    'Authorization': 'JWT ' + token
                 },
                    responseType: 'json',
                    // withCredentials: true
                })
                .then(response => {
                  if (response.data.code == 0) {
                    alert('添加购物车成功');
                    this.cart_total_count += response.data.count;

                  }else{
                    // location.href = 'login.html';
                  }
                })
                .catch(error => {
                    console.log(error);
                })
        },
      }
  })